package com.yourcompany.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yourcompany.api.dto.NoticeQueryDTO;
import com.yourcompany.api.entity.SysNotice;
import com.yourcompany.api.mapper.SysNoticeMapper;
import com.yourcompany.api.service.SysNoticeService;
import com.yourcompany.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Override
    public IPage<SysNotice> pageNotices(IPage<SysNotice> page, NoticeQueryDTO queryDTO) {
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(queryDTO.getTitle()), SysNotice::getTitle, queryDTO.getTitle())
                .eq(StringUtils.isNotBlank(queryDTO.getType()), SysNotice::getType, queryDTO.getType())
                .eq(queryDTO.getStatus() != null, SysNotice::getStatus, queryDTO.getStatus())
                .ge(StringUtils.isNotBlank(queryDTO.getStartTime()), SysNotice::getCreateTime, queryDTO.getStartTime())
                .le(StringUtils.isNotBlank(queryDTO.getEndTime()), SysNotice::getCreateTime, queryDTO.getEndTime())
                .orderByDesc(SysNotice::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNotice(SysNotice notice) {
        // 设置初始状态为待发布
        notice.setStatus(0);
        save(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNotice(SysNotice notice) {
        SysNotice existNotice = getById(notice.getId());
        if (existNotice == null) {
            throw new BusinessException("通知不存在");
        }
        
        // 已发布的通知不能修改
        if (existNotice.getStatus() == 1) {
            throw new BusinessException("已发布的通知不能修改");
        }
        
        updateById(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNotice(Long id) {
        SysNotice notice = getById(id);
        if (notice == null) {
            throw new BusinessException("通知不存在");
        }
        
        // 已发布的通知不能删除
        if (notice.getStatus() == 1) {
            throw new BusinessException("已发布的通知不能删除");
        }
        
        removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishNotice(Long id) {
        SysNotice notice = getById(id);
        if (notice == null) {
            throw new BusinessException("通知不存在");
        }
        
        // 只有待发布状态的通知可以发布
        if (notice.getStatus() != 0) {
            throw new BusinessException("只有待发布状态的通知可以发布");
        }
        
        notice.setStatus(1);
        notice.setPublishTime(LocalDateTime.now());
        updateById(notice);
        
        // TODO: 发送通知给相关用户
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeNotice(Long id) {
        SysNotice notice = getById(id);
        if (notice == null) {
            throw new BusinessException("通知不存在");
        }
        
        // 只有已发布状态的通知可以撤回
        if (notice.getStatus() != 1) {
            throw new BusinessException("只有已发布状态的通知可以撤回");
        }
        
        notice.setStatus(2);
        updateById(notice);
    }
} 